package aps.module_Core;

//buildcraft
import buildcraft.energy.EngineStone;
import buildcraft.energy.TileEngine;

//Vanilla Minecraft
import net.minecraft.src.Container;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Slot;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ItemStack;

public class ContainerAPS extends Container
{
		protected int inventorySize = 0;
	
		public TileEntityAPS tileEntity;
		
        public ContainerAPS(TileEntityAPS cargo, InventoryPlayer playerInv)
        {                
        	if(cargo.getInventory() != null)
        		inventorySize = cargo.getInventory().getSizeInventory();
        	tileEntity = cargo;
        }
        
        //dAkshEN3: Added for machines that don't have any slots. Will most likely change them to a non-container GUI later.
        public ContainerAPS(InventoryPlayer playerInv)
        {                
        	addPlayerInventorySlots(playerInv);
        }
        
        //dAkshEN3: Refactored this out of the constructor so that machine slots can be added before the player inventory slots.
        //This function will be called in all containers.
        public void addPlayerInventorySlots(InventoryPlayer playerInv)
        {
        	for(int i = 0; i < 3; i++)
                for(int k = 0; k < 9; k++)
                        addSlotToContainer(new Slot(playerInv, k + i * 9 + 9, 8 + k * 18, 84 + i * 18)); //Changed from addSlot(), just a rename of method - Seph
        	for(int j = 0; j < 9; j++)
                addSlotToContainer(new Slot(playerInv, j, 8 + j * 18, 142));
        }

        @Override
        public boolean canInteractWith(EntityPlayer player) {
                return true;
        }
        
		//Cybrshdw: Added To Fix Shift Click Error and Stack Overflow
    	@Override	
    	public ItemStack transferStackInSlot(EntityPlayer pl, int i)
    	{
            ItemStack itemstack = null;
            Slot slot = (Slot)inventorySlots.get(i);
            if(slot != null && slot.getHasStack())
            {
                ItemStack itemstack1 = slot.getStack();
                itemstack = itemstack1.copy();
                if(i < inventorySize)
                {
                    if(!mergeItemStack(itemstack1, inventorySize, inventorySlots.size(), true))
                    {
                        return null;
                    }
                } else
                if(!mergeItemStack(itemstack1, 0, inventorySize, false))
                {
                    return null;
                }
                if(itemstack1.stackSize == 0)
                {
                    slot.putStack(null);
                } else
                {
                    slot.onSlotChanged();
                }
            }
            return itemstack;
    	}
}